package morenoapp.components.common;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.Timer;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import morenoapp.logging.MorenoLog;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;

/**
 * Proccess hyperlinks events and opens page in browser or email
 * client if notified.
 * 
 * @author dmitry.mamonov
 */
public class BrowserHyperlinkListener implements HyperlinkListener {
    public void hyperlinkUpdate(final HyperlinkEvent e) {

        if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
            //show loading progress
            final URI uri;
            try {
                uri = e.getURL().toURI();
            } catch (URISyntaxException ex) {
                MorenoLog.notifyUnknownException(ex);
                return;
            }
            final JComponent source = (JComponent) e.getSource();
            source.setEnabled(false);
            final Timer timer = new Timer(250, null);
            timer.setInitialDelay(50);
            timer.addActionListener(new ActionListener() {
                int stage = 10;
                public void actionPerformed(ActionEvent e) {
                    stage--;
                    if (stage<=0){
                        timer.stop();
                        source.setEnabled(true);
                    } else {
                        Graphics g = source.getGraphics();
                        MorenoUtil.drawStringAtComponentCenter(g, source, Color.BLACK, -1.0f, I18N.get("aux.browse") + uri.toString()+" ...");
                        g.dispose();
                    }
                }
            });
            timer.start();


            Timer timerBrowse = new Timer(500, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        //open browser
                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    if (uri.toString().startsWith("mailto:")){
                                        Desktop.getDesktop().mail(uri);
                                    } else {
                                        Desktop.getDesktop().browse(uri);
                                    }
                                } catch (IOException ex) {
                                    MorenoLog.notifyException(I18N.get("error.cant_open_file")+" "+uri, ex);
                                }
                            }
                        }).start();
                }
            });
            timerBrowse.setRepeats(false);
            timerBrowse.start();
        }
    }
}
